package com.example.museum.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.museum.common.Result;
import com.example.museum.entity.OrderQueryParam;
import com.example.museum.entity.Orders;
import com.example.museum.service.OrderService;
import com.example.museum.vo.OrdersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单
 */

// OrderController.java
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    
    /**
     * 分页查询订单列表
     * **/
    @GetMapping("/page")
    public Result<Page<OrdersVo>> pagelist(OrderQueryParam param) {

        log.info("查询订单列表参数:{}", param);

        Page<Orders> pageParam = new Page<>(param.getPage(), param.getPageSize());

        Page<OrdersVo> result = orderService.listOrdersPage(pageParam, param);

        return Result.success(result);

    }
    
    /**
     * 获取订单详情
     */
    @GetMapping("/detail/{number}")
    public Result<OrdersVo> detail(@PathVariable String number) {
        OrdersVo orders = orderService.getOrderById(number);
        if (orders == null) {
            return Result.failed("订单不存在");
        }
        return Result.success(orders);
    }
    
    /**
     * 更新订单状态
     */
    @PutMapping("/status/{orderId}")
    public Result<Boolean> updateStatus(
            @PathVariable String orderId,
            @RequestBody Map<String, String> params) {
        
        String status = params.get("status");
        
        if (StringUtils.isBlank(status)) {
            return Result.failed("状态不能为空");
        }
        
        boolean success = orderService.updateOrderStatus(orderId, status);
        
        if (success) {
            return Result.success(true);
        } else {
            return Result.failed("更新失败");
        }
    }
    
    /**
     * 创建订单
     */
    @PostMapping("/create")
    public Result create(@RequestBody Orders orders) {
        Long userId = orders.getUserId();
        if (userId == null) {
            return Result.failed("用户未登录");
        }
        orders.setUserId(userId);
        OrdersVo createdOrder = orderService.createOrder(orders);

        return Result.success(createdOrder);
    }
    
    /**
     * 删除订单
     */
    @DeleteMapping("/delete/{number}")
    public Result<Boolean> delete(@PathVariable String number) {
        boolean success = orderService.deleteOrder(number);
        
        if (success) {
            return Result.success(true);
        } else {
            return Result.failed("删除失败");
        }
    }
    
    /**
     * 支付订单
     */
    @PutMapping("/{orderNumber}/pay")
    public Result<Boolean> payOrder(
            @PathVariable String orderNumber,
            @RequestBody Map<String, String> params) {
        
        String paymentMethod = params.get("paymentMethod");
        
        if (StringUtils.isBlank(paymentMethod)) {
            return Result.failed("支付方式不能为空");
        }
        
        try {
            boolean success = orderService.payOrder(orderNumber, paymentMethod);
            
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("支付失败");
            }
        } catch (Exception e) {
            return Result.failed("支付失败: " + e.getMessage());
        }
    }
}

